package fictional.GUI;

import fictional.Common.CharacterItem;
import fictional.Common.IBL;
import fictional.Common.Response;
import fictional.Common.ShortCharacterItem;
import fictional.Common.Wall;
import fictional.Common.WallItem;

import java.io.IOException;

import org.jdesktop.application.Application;

/**
 * This task adds and deletes wall posts from the logged in 
 * user's wall. 
 */
public class AddDeleteWallPostTask extends DefaultTask
{
	private CharacterItem poster;
    private ShortCharacterItem postedTo;
    private String text;
    private Wall wall;
    private WallItem item;
    
    public AddDeleteWallPostTask(Application application)
	{
		super(application);
	}
    
    // set the add wall post task
    public void setAddTask(IBL BL, CharacterItem poster, ShortCharacterItem postedTo, 
            String text, Wall wall, GUI gui)
    {
        this.BL = BL;
        this.poster = poster;
        this.postedTo = postedTo;
        this.text = text;
        this.wall = wall;
        this.gui = gui;
    }
    
    // set the delete wall post task
    public void setDeleteTask(IBL BL, Wall wall, WallItem item, GUI gui)
    {
        this.BL = BL;
        this.gui = gui;
        this.item = item;
        this.wall = wall;
    }

    @Override
    protected Object doInBackground() throws IOException, InterruptedException
    {
        response = new Response();
        
        // this is an add task
        if (item == null)
        {
            BL.postToWall(poster, postedTo, text, wall, response);
            
            if (response.isIs_Success())
            {
            	gui.setToDraw(true);
                gui.initWall();
            }
            else
            {
            	Thread.sleep(1000);
                errorTitle = "Error Posting To Wall";
            }
        }
        // this is a delete task
        else
        {
        	BL.deleteWallPost(wall, item, response);
            if (response.isIs_Success())
            {
            	gui.initWall();
            }
            else
            {
            	Thread.sleep(1000);
            	errorTitle = "Error Deleting From Wall";
            }
        }
        
        return null;
    }
}
